<?php
/**
 * Created by 权限类.
 * User: 尚金殿
 * Date: 2018/8/20
 * Time: 10:42
 */

namespace app\admin\controller;




use think\Db;
use think\Request;

class RuleController extends CommonController
{
    protected $autoWriteTimestamp = true;
    protected  $pk = 'rule_id';
    protected $mysql = 'rule';
    protected $status = 'rule_status';

    //加载权限列表
    public function lists(){
        //获取权限
        $list = Db::name($this->mysql)->where($this->status,'>',-1)->select();
        //树形结构
        $rule = getTree($list);
        //分配变量
        $this->assign('rule',$rule);
        //加载模版
        return $this->fetch();
    }


    //权限添加
    public function add(){
        if(\request()->isPost()){
            //获取数据
            $data = $this->request->param();

            //判定数据
            if(!$data['rule_name'] || empty($data['rule_name'])){

                return show(0,'权限名称不能为空');
            }

            if(empty($data['rule_url']) || !$data['rule_url']){
                return show(0,'控制器/方法不能为空');
            }

            if(!empty($data['rule_id'])){
                $this->save($data);
            }
            $data['create_time'] = $data['update_time'] = time();

            //入库
            $res = $this->getAdd($data);
            //返回数据
            if(!$res){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            //获取全部权限
            $rule = Db::name($this->mysql)->where($this->status,'>',-1)->select();
            //树形结构
            $rule = getTree($rule);
            //分配数据
            $this->assign('rule',$rule);
            //加载模版
            return $this->fetch();
        }
    }

    //权限编辑
    public function edit(){
        $id = $this->request->param();
        //获取编辑数据
        $res = $this->getEdit($id);
        //获取分类
        $rule = Db::name($this->mysql)->where($this->status,'>',-1)->select();
        $rule = getTree($rule);
        $this->assign(['result'=>$res,'rule'=>$rule]);

        return $this->fetch();
    }




}